package com.thomasmoerman.dci2.pbc

import org.squeryl.PrimitiveTypeMode._
import com.thomasmoerman.dci2.domain._

trait PayBillsQueries {

  def findPerson(name: String): Person

  def findUnpaidBills(debtorId: Long): Iterable[(Bill, Person)]

  def markAsPaid(billId: Long)
  
}

trait PayBillsQueriesSqueryl extends PayBillsQueries {
  this: Tables =>

  def findPerson(name: String) =
    from(persons)(p =>
      where(p.firstName === name or p.lastName === name)
      select(p)).single

  def findUnpaidBills(debtorId: Long) =
    from(bills, persons)((b, p) =>
      where(b.creditorId === p.id and b.debtorId === debtorId and b.paid === false)
      select((b, p)))

  def markAsPaid(billId: Long) =
    update(bills)(b =>
      where(b.id === billId)
      set(b.paid := true))
  
}